package edu.jsnu.purchase.ticket.controller.ticket;

import dto.resDto.JsonData;
import edu.jsnu.purchase.ticket.controller.common.AbstractController;
import edu.jsnu.purchase.ticket.service.OrderService;
import edu.jsnu.purchase.ticket.service.UserService;
import edu.jsnu.service.ticket.order.domain.Order;
import edu.jsnu.service.ticket.order.dto.OrderVo;
import edu.jsnu.service.ticket.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/ticket/user")
public class UserController extends AbstractController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @RequestMapping("/orderInfo.page")
    public String orderInfoPage() {
        return "ticket/user/orderInfo";
    }

    @RequestMapping("/userInfo.page")
    public String userInfo(Model model, @RequestParam(value = "userId") Integer userId){
        User user = userService.getUserById(userId);
        model.addAttribute("user", user);
        return "/ticket/user/userInfo";
    }

    @RequestMapping(value = "recharge.page")
    public String recharge(){
        return "/ticket/user/recharge";
    }

    @RequestMapping("order.do")
    @ResponseBody
    public JsonData orderData(HttpServletRequest request) {
        try {
            User user = (User) request.getSession().getAttribute("user");
            List<OrderVo> orderList = orderService.getUserOrderInfo(user.getId());
            return JsonData.success(orderList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("获取订单信息异常，请联系开发人员");
    }

    @RequestMapping("reback.do")
    @ResponseBody
    public JsonData reBack(@RequestParam(value = "orderId") Integer orderId) {
        try {
            orderService.updateOrderStatusByUser(orderId, 2);
            return JsonData.success("退票成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("获取订单信息异常，请联系开发人员");
    }

    @RequestMapping(value = "pay.do", method = RequestMethod.POST)
    @ResponseBody
    public JsonData payMoney(
            @RequestParam(value = "userId") Integer userId,
            @RequestParam(value = "scheduleId") Integer scheduleId,
            @RequestParam(value = "filmId") Integer filmId,
            @RequestParam(value = "quantity") Integer quantity ,
            @RequestParam(value = "money") String money,
            @RequestParam(value = "seatNo") String seatNo,
            @RequestParam(value = "seatNumber") String seatNumber
    ) {
        try {
            //下订单
            Order order = orderService.insertOrder(userId, scheduleId, filmId, quantity, Double.valueOf(money), seatNo);
            if (order != null){
                return userService.paySuccess(order.getId(), userId, Double.valueOf(money), seatNumber, scheduleId) ? JsonData.success("付款成功") : JsonData.fail("余额不足，请先充值");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("余额不足，请先充值");
    }

    @RequestMapping(value = "save.do", method = RequestMethod.POST)
    @ResponseBody
    public JsonData userInfo(HttpServletRequest request, User user){
        try{
            Integer result = userService.updateUser(user);
            if (result > 0){
//                request.getSession().removeAttribute("user");
                return JsonData.success("保存用户信息成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonData.fail("保存用户信息异常，请稍后重试");
        }
        return JsonData.fail("保存用户信息失败，请稍后再试");
    }

    @RequestMapping(value = "recharge.do")
    @ResponseBody
    public JsonData doRecharge(HttpServletRequest request,
                               @RequestParam(value = "id")Integer id,
                               @RequestParam(value = "money")Double money,
                               @RequestParam(value = "password")String password){
        try{
            User user = (User) request.getSession().getAttribute("user");
            if (!user.getPassword().equals(password)){
                return JsonData.fail("密码错误！");
            } else {
                User chargeUser = userService.recharge(id, money);
                if (chargeUser != null){
                    request.getSession().removeAttribute("user");
                    request.getSession().setAttribute("user", chargeUser);
                    return JsonData.success(chargeUser);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonData.fail("充值异常，请稍后重试");
        }
        return JsonData.fail("充值失败，请稍后重试");
    }

}
